//
//  MBDGroupMainCellModel.swift
//  MasterBoy
//
//  Created by kyson on 2024/9/20.
//

import Foundation


import YYText
import SnapKit
import Alamofire
import SwiftyJSON
import Toast_Swift
import MJRefresh
import Foundation
import JLRoutes


class MBDMainCellModel : MBDCommonCellModel {
    var titlelayout : YYTextLayout!
    var subtitlelayout : YYTextLayout!
    var uploaderlayout : YYTextLayout!
    var commentsCount : String!
    var praiseCount : String!
    var avatarURL : String!
    
    var posterAvatarClicedBlock : KPosterAvatarClickedBlock?
    
    var cellHeight : CGFloat!
    
    static let topMargin :CGFloat = 15
    static let leftMargin :CGFloat = 15
    static let uploaderAvatarHeight :CGFloat = 32
    static let marginBetweenAvatarAndTitle :CGFloat = 10
    static let marginBetweenTitleAndSubtitle :CGFloat = 8
    static let marginSubtitleLeft :CGFloat = 15
    static let marginSubtitleRight :CGFloat = 15
    //图片保持 16:9
    static let groupCoverHeight :CGFloat = (UIScreen.main.bounds.width - MBDMainCellModel.leftMargin * 2 ) * MBDPickerConfig.ratioGroupLogoHeightWidth

    static let groupCoverMarginTop :CGFloat = 6
    static let groupCoverMarginBottom :CGFloat = 6
    
    static let praiseButtonHeight :CGFloat = 25
    
    
    var groupDetail : MBDGroupDetailModel! {
        didSet {
            self.title = self.groupDetail.groupName
            
            let gpdsc = self.groupDetail.groupDesc.replacingOccurrences(of: "\n", with: "")
            //todo:这里有个问题，替换空格不起作用
            var gpdscWithoutSpace = gpdsc.replacingOccurrences(of: " ", with: "")
            let gpdscWithoutTab = gpdscWithoutSpace.replacingOccurrences(of: "\t", with: "")

            self.subtitle = gpdscWithoutTab
            self.groupCoverURL = MBDPath2URLUtils.wrapGroupLogoURL(path: self.groupDetail.groupLogo)
            self.avatarURL = MBDPath2URLUtils.wrapAvatarURL(path: self.groupDetail.groupPosterAvatar)
            
            let postTime = MBDTimeUitls.time2CurrentTime(time: self.groupDetail.createTime)
            let groupPoster = self.groupDetail.groupPoster ?? "无名氏"
            self.uploader = groupPoster.appending("  ").appending(postTime)
            
            self.commentsCount = Int(self.groupDetail.groupCommentCount) ?? 0 > 0 ? self.groupDetail.groupCommentCount : ""
            self.praiseCount = Int(self.groupDetail.likeCount) ?? 0 > 0 ? self.groupDetail.likeCount : ""
            
            self.posterAvatarClicedBlock = {
                let params : Dictionary = ["avatarURL":self.avatarURL ?? "" ,"realName":groupPoster,"userId":self.groupDetail.userId]

                JLRoutes.routeURL(URL(string: "userInfo"),withParameters: params as [String : Any])
            }
        }
    }
    
    var title: String? {
        didSet {
            
            let titleAttributeString = NSMutableAttributedString(string: self.title ?? "")
            titleAttributeString.yy_font = UIFont.boldSystemFont(ofSize: 18)
            
            titleAttributeString.setDynamicTextColor()
            
            let titlecontainer = YYTextContainer()
            let titleWidth = CGRectGetWidth(UIScreen.main.bounds) - 15 - 60 - 15 - 15
            titlecontainer.size = CGSizeMake(titleWidth , CGFLOAT_MAX)
            titlecontainer.maximumNumberOfRows = 1
            
            self.titlelayout = YYTextLayout(container: titlecontainer, text: titleAttributeString)
            self.updateCellHeight()
        }
    }
    
    var subtitle: String? {
        didSet {
            
            let subtitleAttributeString = NSMutableAttributedString(string: self.subtitle ?? "")
            subtitleAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            subtitleAttributeString.yy_lineSpacing = 4
            
            subtitleAttributeString.setDynamicTextColor()
            
            let subtitleContainer = YYTextContainer()
            let subtitleWidth = CGRectGetWidth(UIScreen.main.bounds) - MBDMainCellModel.marginSubtitleLeft - MBDMainCellModel.marginSubtitleRight
            subtitleContainer.size = CGSizeMake(subtitleWidth , CGFLOAT_MAX)
            subtitleContainer.maximumNumberOfRows = 3
            
            self.subtitlelayout = YYTextLayout(container: subtitleContainer, text: subtitleAttributeString)
            
            self.updateCellHeight()
        }
    }
    
    var uploader: String? {
        didSet {
            let uploaderAttributeString = NSMutableAttributedString(string: self.uploader ?? "")
            uploaderAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            uploaderAttributeString.yy_lineSpacing = 4
            
            uploaderAttributeString.setDynamicTextColor()
            
            let uploaderContainer = YYTextContainer()
            let uploaderWidth = CGRectGetWidth(UIScreen.main.bounds) - MBDMainCellModel.marginSubtitleLeft - MBDMainCellModel.marginSubtitleRight
            uploaderContainer.size = CGSizeMake(uploaderWidth , CGFLOAT_MAX)
            uploaderContainer.maximumNumberOfRows = 1
            
            self.uploaderlayout = YYTextLayout(container: uploaderContainer, text: uploaderAttributeString)
            
            self.updateCellHeight()
        }
    }
    
    var groupCoverURL : String? {
        didSet {
            self.updateCellHeight()
        }
    }

    func updateCellHeight(){
        var cellHeight = MBDMainCellModel.topMargin
        cellHeight += MBDMainCellModel.uploaderAvatarHeight
        
        if let tl = self.titlelayout {
            cellHeight += tl.textBoundingSize.height
            
            cellHeight += MBDMainCellModel.marginBetweenAvatarAndTitle
        }
        
        if let stl = self.subtitlelayout {
            cellHeight += stl.textBoundingSize.height
            
            cellHeight += MBDMainCellModel.marginBetweenTitleAndSubtitle
        }
        
        if let cover = self.groupCoverURL {
            cellHeight += MBDMainCellModel.groupCoverMarginTop
            cellHeight += MBDMainCellModel.groupCoverHeight
            cellHeight += MBDMainCellModel.groupCoverMarginBottom
        }
        
        cellHeight += MBDMainCellModel.praiseButtonHeight
        
        cellHeight += 10
        
        self.cellHeight = cellHeight
    }
    
}
